package com.koron.system.orderAsset.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.koron.bean.query.PageQuery;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.util.CodeTools;
import com.koron.system.orderAsset.bean.OrderAsset;
import com.koron.system.orderAsset.mapper.OrderAssetMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class OrderAssetServiceImpl implements OrderAssetService {

    public static final String ORDERASSET_PREFIX = "GDZC";

    @Autowired
    private OrderAssetMapper orderAssetMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    @Override
    public void insertBatchOrderAsset(List<OrderAsset> orderAssets) {

        try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);) {
            OrderAssetMapper mapper = sqlSession.getMapper(OrderAssetMapper.class);
            List<List<OrderAsset>> partition = Lists.partition(orderAssets, 500);

            for (List<OrderAsset> assetList : partition) {
                mapper.insertBatchOrderAsset(assetList);
            }
            sqlSession.commit();
        } catch (Exception e) {
            log.error("OrderAssetServiceImpl insertBatchOrderAsset has error!");
        }
    }

    @Override
    public int updateOrderAsset(OrderAsset orderAsset) {
       return orderAssetMapper.updateOrderAsset(orderAsset);
    }

    @Override
    public OrderAsset selectOrderAssetById(String id) {
        return orderAssetMapper.selectOrderAssetById(id);
    }

    @Override
    public String insertOrderAsset(OrderAsset orderAsset) {
        orderAsset.setId(CodeTools.getCode32());
        EamUser eamUser = ThreadLocalContext.get();
        orderAsset.setCreateBy(eamUser.getCreateBy());
        orderAsset.setCreateByName(eamUser.getCreateByName());
        orderAsset.setCreateTime(new Date());
        orderAsset.setDeleteFlag(0);
        orderAsset.setCode(CodeTools.getCodeNo(ORDERASSET_PREFIX, 3));
        int result = orderAssetMapper.insertOrderAsset(orderAsset);
        Assert.isTrue(result > 0,"新增工单资产信息失败！");
        return orderAsset.getId();
    }

    @Override
    public int deleteOrderAsset(String id) {
        return orderAssetMapper.deleteOrderAsset(id);
    }

    @Override
    public PageInfo selectByOrderAssetQuery(OrderAsset orderAsset, PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPage(),pageQuery.getPageSize());

        List<OrderAsset> orderAssets = orderAssetMapper.selectByOrderAssetQuery(orderAsset);

        return new PageInfo<>(orderAssets);
    }

}
